<template>
  <div>
    <van-swipe-cell v-for="(item, index) in user" :key="index">
      <van-card
          class="goods-card"
          :thumb="item.img"
      >
        <template #title>
          <span style="font-size: 24px;line-height: 35px">{{ item.name }}</span>
          <p style="line-height: 30px">账号:{{ item.account }}</p>
        </template>
      </van-card>
      <template #right>
        <van-button v-show="isChart" square text="聊天" type="info" class="button" @click="goChart(item.id)"/>
        <van-button square text="主页" type="primary" class="button" @click="goUserDetail(item.id)"/>
        <van-button v-show="isFollow" v-if="item.isFollowed" square text="取关" type="danger" class="button"
                    @click="follow(item, 0)"/>
        <van-button v-show="isFollow" v-else square text="关注" type="info" class="button" @click="follow(item, 1)"/>
      </template>
    </van-swipe-cell>
  </div>
</template>

<script>
import {Toast} from "vant";

export default {
  props: ["user","isChart","isFollow"],
  methods: {
    goChart(id) {
      this.$router.push({path: '/chat', query: {id: id}});
    },
    goUserDetail(id) {
      this.$router.push({path: '/homePage', query: {id: id}});
    },
    follow(user, type) {
      let url = `/api/mfh/user/follow?followedId=${user.id}&type=${type}`;
      this.http.get(url).then(() => {
        if (type === 0) {
          user.isFollowed = false;
          Toast("取关成功");
        } else {
          user.isFollowed = true;
          Toast("关注成功");
        }
      })
    },
  }
}
</script>

<style scoped>
.goods-card {
  margin: 0;
  background-color: whitesmoke;
}

.button {
  height: 100%;
}
</style>